using System;

using Portal.Data.Entity;

namespace Portal.Data.Webparts
{
    public class BrowseModeWebpartLoader : IWebpartLoader
    {
        #region IWebpartContainer Members

        System.Web.UI.Page _page;

        public System.Web.UI.Page WebPage
        {
            set
            {
                _page = value;
            }
        }

        public System.Web.UI.Control Contain(Webpart webpart)
        {
            WebpartControl wpControl;
            try
            {
                wpControl = (WebpartControl)_page.LoadControl(webpart.Definition().Path);
            }
            catch (Exception ex)
            {
                wpControl = new WebpartControl();
                var ltr = new System.Web.UI.WebControls.Literal
                {
                    Text =
                        "<div style='background-color:#ffffff;border:solid 1px red;color:red'>" +
                        ex.Message + "</div>"
                };
                wpControl.Controls.Add(ltr);
            }
            wpControl.ViewMode = PageViewMode.Browse;
            wpControl.Info = webpart;

            return wpControl;
        }

        #endregion
    }
}
